<?php
	namespace Home\Model;
	use Think\Model;
	
	class GoodsModel extends CommonModel
	{
		
		public function showclassify()
		{

			// 面包线
			$cid = I('get.id');
			$classify = M('classify');
			$map['id'] = ['eq' , $cid];
			$path = $classify->field('path , name')->where($map)->select()[0];

			$bread = [];
			$num = substr_count($path['path'] , ',');
			// 如果是顶级为1 二级为2 三级为3
			if ($num == 1) {
				$bread[0] = $path['name'];
			}else if($num == 2){
				$pa = explode("," , $path['path'] );
				$map['id'] = ['eq' , $pa[1]];
				$bread[0] = $classify->field('name')->where($map)->select()[0]['name'];
				$bread[1] = $path['name'];

			}else{
				$pa = explode("," , $path['path'] );
				

				$map['id'] = ['eq' , $pa[1]];
				$bread[0] = $classify->field('name')->where($map)->select()[0]['name'];

				$map['id'] = ['eq' , $pa[2]];
				$bread[1] = $classify->field('name')->where($map)->select()[0]['name'];

				$bread[2] = $path['name'];
			}

			$bread = implode('->',$bread); 
			
			// 面包线结束


			$ids = $this->queryGoods(I('get.id'));

			$kkk = I('get.id');

			$goodsid = I('get.goodsid');

			// 添加购物车的操作
			if (!empty($goodsid)) {

				$user_id = I('get.user_id');

				// 判断该商品是否已经存在购物车中
				$cart = M('cart');
				$mapcart['user_id'] = $user_id ;
				$mapcart['goods_id'] = $goodsid;
				$mapcart['size'] = 'M';
				$samecart = $cart->where($mapcart)->select();
				$num = 1;

				

				// 进行判断
				if (count($samecart)) {
					$rescart = $cart->where($mapcart)->setInc('num' , $num);

					// 购物车的数量
			        $user_id = I('session.')['onethink_home']['user_id'];
			        $cart = M('cart');
			        $map1['user_id'] =  $user_id;
			        $map1['status'] = 0;
			        // $map1['goods_id'] = $goodsid;
			        $shop1 = $cart->where($map1)->select();
			        $shopcount = 0;
			        foreach ($shop1 as $countkey => $countval) {
			            $shopcount += $countval['num'];
			        }
					echo $shopcount;
				}else{

					// 当之前没有添加的时候
					$sql = "select * from north_goods as a inner join north_image as b on a.goods_id=b.goods_id and a.goods_id={$goodsid}";

					$res = $this->query($sql)[0];
					$goodsname = $this->field('name')->where('goods_id = ' . $goodsid )->select();

					$res['user_id'] = $user_id;
					$res['size'] = 'M';
					$res['num'] = 1;
					$imagepath1 = $res['path'] . $res['name'];
					$res['goods_name'] = $goodsname[0]['name'];
					$cart = M('cart');
					$res = $cart->create($res);
					$res['image'] = 'Uploads/' . ltrim($imagepath1 , './');
					$res['ctime'] = time();
					$res['price'] = &explode(".",$res['price'])[0];
					$result = $cart->add($res);

					 // 购物车的数量
			        $user_id = I('session.')['onethink_home']['user_id'];
			        $cart = M('cart');
			        $map1['user_id'] =  $user_id;
			        $map1['status'] = 0;
			        $shop1 = $cart->where($map1)->select();

			        $shopcount = 0;
			        foreach ($shop1 as $countkey => $countval) {
			            $shopcount += $countval['num'];
			        }
					echo $shopcount;
				}


				
			};



			//查询商品			
			if (!empty($ids)) {
				$id = " and goods_id in ($ids) ";
				$reportid = " goods_id in ($ids) ";
			}else{
				$id = ' and goods_id in (0) ';
				$reportid = " goods_id in (0) ";
			}


			// 搜索框
			$search = '';

			// 搜索框
			if (empty(I('post.ipt'))) {
				$search = '';
			}else{
				$ipt = I('post.ipt');
				$search = " and name like '%$ipt%' ";
			}


			// 分类页的搜索
			$get1 = empty(I('get.choose')) ?  '' : I('get.choose');
			$get = empty(I('get.page')) ?  '' : I('get.page');

			if (empty($get) && empty($get1)) {
				session('search' , null);
			}else{
				$sea = I('session.')['onethink_home']['search'];
				$search = " and name like '%$sea%' ";

			}

			// 获取输入框的值,存进session
			session('search', I('post.ipt') );


			$ss = I('session.')['onethink_home']['search'];


			switch ($get) {
				case 'priceup':
					$choose = ' price desc ';
					break;
				case 'pricedown':
					$choose = ' price asc ';
					break;
				case 'addtime':
					$choose = ' addtime desc ';
					break;
				default:
					$choose = ' addtime desc ';
					break;
			}

			$count = $this->where('status = ' . 1 . $id  . $search )->count();
			$pagenum = ceil($count / 9);

			$per = 9;
			$Page = new \Common\Controller\Page($count,$per);
			//4. 获得页码列表
        	$show = $Page -> fpage();

			$sql = "select * from `north_goods`  where `status` = '1' $id $search order by $choose  " . $Page->limit ;

			$data = $this->query($sql);

			// 获取商品的热销
			$report = M('report');
			$reportlist = $report->where( $reportid )->order('views desc')->limit(0,4)->select();

			$sale = [];
			$salenum = [];
			foreach ($reportlist as $key1 => $vv) {
				// $repid .= $vv['goods_id'] . ',';


				$hot[$key1] = $this->where('goods_id = ' .  $vv['goods_id'])->select();

				$hot[$key1][0]['price'] = explode("." , $hot[$key1][0]['price'])[0];
	
				$img1 = M('image');

				//获取热销图片
				$hotsameimg = $img1->where('goods_id = ' . $vv['goods_id'])->select()[0];

				// // 拼接图片路径
				$hot[$key1]['hotsmallimg'] = ltrim($hotsameimg['path'] , '.') . $hotsameimg['name'];

				// 销量
				$attr = M('attr');
				$sale = $attr->where('goods_id = ' .  $vv['goods_id'])->select();

				foreach ($sale as $salekey => $saleval) {
					$hot[$key1]['sale'] += $saleval['sale'];
				}

			}



			$img = M('image');
			$imglist = [];
			$color = [];
			$sameimg = [];
			foreach ($data as $key => &$value) {
				// ÇÐ¸î¼Û¸ñµÄÃû×Ö
				$value['price'] = explode("." , $value['price']);
				
				// ¼¸ÖÖÑÕÉ«
				$map['name'] = ['eq' , $value['name']];
				$color = $this->where($map)->select();

				// Èç¹ûÏàÍ¬Ãû×ÖÓÐ¶àÖÖÑÕÉ«Ôò¼ÆËãÓÐ¼¸ÖÖ
				if (count($color) > 1) {
					// °ÑÑÕÉ«ÊýÁ¿ÒýÓÃ½ø·µ»ØÖµ
					$value['numcolor'] = count($color);

					// ±éÀúÏàÍ¬ÑÕÉ«µÄÍ¼Æ¬
					foreach ($color as $ke => $val) {

						// ÕÒµ½Í¼Æ¬µÄ±í
						$sameimg[$ke] = $img->where("goods_id = {$val['goods_id']} and status in (1)")->select()[0];
						
						// Æ´½ÓÍ¼Æ¬Â·¾¢
						$value['smallimg'][$ke] = ltrim($sameimg[$ke]['path'] , '.') . $sameimg[$ke]['name'];
					}
				}

				// ÕÒµ½Í¼Æ¬µÄ±í
				$imglist[$key] = $img->where("goods_id = {$value['goods_id']} and status in (1)")->select();			
			}

			foreach($data as $k=>&$v){
				// $v['image_path'] = ltrim($imglist[$k][0]['path'],'.').$imglist[$k][0]['name'];
				$v['image_path'] = ltrim($imglist[$k][0]['path'],'.').$imglist[$k][0]['name'];
			}

			// ÈÈÏúÉÌÆ·
			// echo '<pre>';
			// 	print_r($imglist);
			// echo '<pre>';
						
			return array('goods' => $data , 'img' => $imglist , 'numcolor' => $numcolor , 'sameimg' => $sameimg , 'show' => $show , 'count' => $count , 'pagenum' => $pagenum , 'emp' => $emp , 'hot' => $hot , 'kkk' => $kkk , 'bread' => $bread , 'ss' => $ss , 'bigimg' => $imglist);
		}

		// 访问量
		public function addreport()
		{
			$goods_id = I('get.id');

			$report = M('report');
			$res = $report->where('goods_id = ' . $goods_id)->setInc('views' , 1);
			return $res;
		}


	}

